//
//  GroupGuestLogic.m
//  QRMaster
//
//  Created by myApple on 15/12/22.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "GroupGuestLogic.h"
#import "OperationDataBaseModel.h"

@interface GroupGuestLogic()
{
    OperationDataBaseModel * operation;
}

@end

@implementation GroupGuestLogic

#pragma mark ---------------------插入组内客户的数据
-(void)insertGroupGuestInfo:(GuestModel *)model groupName:(NSString *)_groupName groupStartTime:(NSString *)_startTime groupEndTime:(NSString *)_endTime groupId:(NSString *)_groupId
{
    NSMutableDictionary * staffDict = [self modelConvertDict:model];
    [staffDict setValue:[self getStr:_groupName] forKey:@"groupName"];
    [staffDict setValue:[self getStr:_startTime] forKey:@"groupStartTime"];
    [staffDict setValue:[self getStr:_endTime] forKey:@"groupEndTime"];
    [staffDict setValue:[self getStr:_groupId] forKey:@"groupId"];
    [staffDict setValue:[self getStr:model.guestid] forKey:@"id"];
    [staffDict setValue:[self getStr:model.guestName] forKey:@"name"];
    [staffDict setValue:[self getStr:model.sex] forKey:@"gender"];
    [staffDict setValue:[self getStr:model.headUrl] forKey:@"headurl"];
    [staffDict setValue:[self getStr:model.telePhone] forKey:@"mobile"];
    [staffDict setValue:[self getStr:model.email] forKey:@"email"];
    [staffDict setValue:[self getStr:model.certificaties] forKey:@"identity"];
    [staffDict setValue:[self getStr:model.isGroupManager] forKey:@"isGroupManager"];
    [staffDict setValue:[self getStr:model.firstChar] forKey:@"firstchar"];
    [staffDict setValue:[self getStr:model.areaCode] forKey:@"areaCode"];
    operation = [[OperationDataBaseModel alloc]init];
    [operation insertDataTable:@"groupMembers" withDict:staffDict];
}

#pragma mark ---------------------查询组内客户的数据
-(void)queryGroupGuestInfo:(NSString *)groupId block:(groupGuestResult)groupGuestArray
{
    __block NSMutableArray * groupGuestMutableArray = [[NSMutableArray alloc]init];
    __weak GroupGuestLogic * weakSelf = self;
    NSString * condition = [NSString stringWithFormat:@"where %@=%@",@"groupId",groupId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation queryDataFromTable:@"groupMembers" withCondition:condition block:^(FMResultSet *set) {
        while ([set next]) {
            //取值
            NSString * guestId = [set stringForColumn:@"id"];
            NSString * name = [set stringForColumn:@"name"];
            NSString * gender = [set stringForColumn:@"gender"];
            NSString * headurl = [set stringForColumn:@"headurl"];
            NSString * firschar = [set stringForColumn:@"firstchar"];
            NSString * mobile = [set stringForColumn:@"mobile"];
            NSString * email = [set stringForColumn:@"email"];
            NSString * identity = [set stringForColumn:@"identity"];
            NSString * isGroupManager = [set stringForColumn:@"isGroupManager"];
            NSString * groupName = [set stringForColumn:@"groupName"];
            NSString * groupStartTime = [set stringForColumn:@"groupStartTime"];
            NSString * groupEndTime = [set stringForColumn:@"groupEndTime"];
            NSString * groupId = [set stringForColumn:@"groupId"];
            NSString * areaCode = [set stringForColumn:@"areaCode"];
            
            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
            [dic setObject:guestId forKey:@"id"];
            [dic setObject:[weakSelf getStr:name] forKey:@"name"];
            [dic setObject:[weakSelf getStr:gender] forKey:@"gender"];
            [dic setObject:[weakSelf getStr:headurl] forKey:@"headurl"];
            [dic setObject:[weakSelf getStr:firschar] forKey:@"firschar"];
            [dic setObject:[weakSelf getStr:mobile] forKey:@"mobile"];
            [dic setObject:[weakSelf getStr:email] forKey:@"email"];
            [dic setObject:[weakSelf getStr:identity] forKey:@"identity"];
            [dic setObject:[weakSelf getStr:isGroupManager] forKey:@"isGroupManager"];
            [dic setObject:[weakSelf getStr:groupName] forKey:@"groupName"];
            [dic setObject:[weakSelf getStr:groupStartTime] forKey:@"groupStartTime"];
            [dic setObject:[weakSelf getStr:groupEndTime] forKey:@"groupEndTime"];
            [dic setObject:[weakSelf getStr:groupId] forKey:@"groupId"];
            [dic setObject:[weakSelf getStr:areaCode] forKey:@"areaCode"];
            
            [groupGuestMutableArray addObject:dic];
        }
        if (groupGuestArray) {
            groupGuestArray(groupGuestMutableArray);
        }
    }];
}

#pragma mark -----------------删除组内客户信息
-(void)deleteGroupGuestInfo:(NSString *)groupId groupGuest:(NSString *)guestId
{
    NSString * condition = [NSString stringWithFormat:@"where id in %@ and groupId = %@",[NSString stringWithFormat:@"(%@)",guestId],groupId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation deleteDataFromTable:@"groupMembers" withCondition:condition];
}

#pragma mark ----------------通过组ID号删除组内所有客户
-(void)deleteGroupGuestInfoByGroupId:(NSString *)_groupId
{
    NSString * condition = [NSString stringWithFormat:@"where groupId = %@",_groupId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation deleteDataFromTable:@"groupMembers" withCondition:condition];
}


#pragma mark ------------------更新组内客户信息
-(void)updateGroupGuestInfo:(GuestModel *)model groupId:(NSString *)_groupId groupName:(NSString *)_groupName groupStartTime:(NSString *)_startTime groupEndTime:(NSString *)_endTime
{
    operation = [[OperationDataBaseModel alloc]init];
    if ([model.isGroupManager isEqualToString:@"1"]) {
        //组管理员
        NSMutableDictionary * managerDict = [[NSMutableDictionary alloc]init];
        [managerDict setValue:_groupName forKey:@"groupName"];
        [managerDict setValue:_startTime forKey:@"groupStartTime"];
        [managerDict setValue:_endTime forKey:@"groupEndTime"];
        NSString * managerCondition = [NSString stringWithFormat:@"where groupId = %@",_groupId];
        [operation updateDataFromTable:@"groupMembers" withCondition:managerCondition withDict:managerDict];
    }
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
    [dict setValue:_groupName forKey:@"groupName"];
    [dict setValue:model.telePhone forKey:@"mobile"];
    [dict setValue:model.email forKey:@"email"];
    [dict setValue:model.certificaties forKey:@"identity"];
    [dict setValue:model.guestName forKey:@"name"];
    [dict setValue:model.sex forKey:@"gender"];
    NSString * condition = [NSString stringWithFormat:@"where id = %@ and groupId = %@",model.guestid,_groupId];
    [operation updateDataFromTable:@"groupMembers" withCondition:condition withDict:dict];
}

#pragma mark -----查询组内客户时间戳
-(NSString *)queryGroupGuestTime:(NSString *)groupId
{
    __block NSString * str;
    OperationDataBaseModel *opData = [[OperationDataBaseModel alloc]init];
    [opData queryDataFromTable:@"groupInfo" withCondition:[NSString stringWithFormat:@"where groupId = %@",groupId] block:^(FMResultSet *set) {
        while ([set next]) {
            str = [set stringForColumn:@"groupClient_timestamp"];
        }
    }];
    return str;
}

#pragma mark -----更新组内客户时间戳
-(void)updateGroupGuestTime:(NSString *)groupId keyValue:(NSString *)keyValue
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:keyValue forKey:@"groupClient_timestamp"];
    OperationDataBaseModel *opData = [[OperationDataBaseModel alloc]init];
    [opData updateDataFromTable:@"groupInfo" withCondition:[NSString stringWithFormat:@"where groupId = %@",groupId] withDict:dic];
}
/**
 *  模型转化为字典
 *
 *  @param model 组客户模型
 *
 *  @return 可变字典
 */
-(NSMutableDictionary *)modelConvertDict:(GuestModel *)model
{
    NSMutableDictionary * staffDict = [[NSMutableDictionary alloc]init];
    [staffDict setValue:model.guestid forKey:@"id"];
    [staffDict setValue:model.telePhone forKey:@"mobile"];
    [staffDict setValue:model.email forKey:@"email"];
    [staffDict setValue:model.guestName forKey:@"name"];
    [staffDict setValue:model.sex forKey:@"gender"];
    [staffDict setValue:model.certificaties forKey:@"identity"];
    [staffDict setValue:model.firstChar forKey:@"firstChar"];
    [staffDict setValue:model.headUrl forKey:@"headUrl"];
    [staffDict setValue:model.isGroupManager forKey:@"isGroupManager"];
    return staffDict;
}


-(NSString *)getStr:(NSString *)str
{
    NSString *returnStr=@"";
    if (str==nil || [@"" isEqualToString:str]==YES || str.length==0 ) {
        returnStr=@"";
    }
    else
    {
        returnStr=str;
    }
    return returnStr;
}


@end
